/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/***********************************************************************
 *
 * FILENAME:
 * vcConfig.c
 *
 * PURPOSE:
 *   Functional interfaces to configure the VC.
 *
 * CREATED:
 * 02/2005 by Cory Dixon
 *
 * LAST MODIFIED:
 * $Author: dixonc $
 * $Date: 2005/07/13 20:34:40 $
 * $Revision: 1.6 $
 *
 ***********************************************************************/

#include <glib.h>
#include <gconf/gconf-client.h>

#include "vcConfig.h"
#include "myStripChart.h"
#include "color.h"

extern GuiStruct guiStr;

void vc_config_free ()
{
	g_object_unref(guiStr.client);
	guiStr.client = NULL;
}

/*************************************************
*
* VC Data
*/
void vc_config_init(int argc, char * argv[])
{
	// initialzie g-conf
	gconf_init (argc, argv, NULL);

	guiStr.client = gconf_client_get_default ();
	if( guiStr.client == NULL ) {
		cout << "gconf_client_get_default: error did not get pointer" << endl;
		return;
	}

	printf("Configuring GConf client strings: ");
	printf("."); fflush(stdout);

	// units
	guiStr.config.units = SI_M_U; 

	// GPS Map
	int num_maps = gconf_client_get_int(guiStr.client, "/apps/vcgui/raster/num_maps", NULL);

	guiStr.config.map_files = NULL;

	char *tmpStr;
	char *str;
	if( num_maps > 0 ) {
		for(int i=0; i < num_maps; i++ ) {
			tmpStr  = g_strdup_printf("/apps/vcgui/raster/%i", i);
			str = gconf_client_get_string (guiStr.client, tmpStr, NULL);
			guiStr.config.map_files = g_list_append( guiStr.config.map_files, str);
			g_free(tmpStr);
		}
	}
	
	// configuration files
	guiStr.config.glade_file         = gconf_client_get_string (guiStr.client, "/apps/vcgui/glade_file", NULL);
	guiStr.config.menu_ui_file       = gconf_client_get_string (guiStr.client, "/apps/vcgui/menu_ui_file", NULL);
	guiStr.config.radar_station_file = gconf_client_get_string (guiStr.client, "/apps/vcgui/radar_station_file", NULL);
	guiStr.config.node_ui_file       = gconf_client_get_string (guiStr.client, "/apps/vcgui/node_ui_file", NULL);
	guiStr.config.width              = gconf_client_get_int (guiStr.client, "/apps/vcgui/width", NULL);
	guiStr.config.height             = gconf_client_get_int (guiStr.client, "/apps/vcgui/height", NULL);

	if( guiStr.config.glade_file == "" ) 
		guiStr.config.glade_file = VCGUI_DIR"/gui/vcgui3.xml";
	if( guiStr.config.menu_ui_file == "" ) 
		guiStr.config.menu_ui_file = VCGUI_DIR"/gui/vcgui3_menu_ui.xml";
	if( guiStr.config.radar_station_file == "" ) 
		guiStr.config.radar_station_file = VCGUI_DIR"/radar/nexrad-stations.txt";
	if( guiStr.config.node_ui_file == "" ) 
		guiStr.config.node_ui_file = VCGUI_DIR"/gui/node_window.xml";

	//cout << "vc_config_init -- "  << endl
	     //<< "guiStr.config.glade_file=" << guiStr.config.glade_file << endl
	     //<< "guiStr.config.menu_ui_file=" << guiStr.config.menu_ui_file << endl
	     //<< "guiStr.config.radar_station_file=" << guiStr.config.radar_station_file << endl
	     //<< "guiStr.config.node_ui_file=" << guiStr.config.node_ui_file << endl;

	// main window size, with bounds
	if( guiStr.config.width <= 0 || guiStr.config.width > 2048)
		guiStr.config.width = 600;
	if( guiStr.config.height <= 0 || guiStr.config.height > 2048)
		guiStr.config.height = 700;

	//------------------
	// Somtimes, when someone screws up (I have done it), the window gets bigger
	// than your screen. When this happens you can't change the size of the
	// window easily (I happen to have key maps to resize windows). So, instead
	// you can turn these two options on and recompile to force the window size.
	//------------------
	//guiStr.config.width = 660;
	//guiStr.config.height = 730;
	//printf(" width=%i height=%i", guiStr.config.width, guiStr.config.height);
	
	printf(".............");

	printf(" [%sdone%s]\n", BLUE, ATTROFF);
}

void vc_config_save ()
{
	GConfClient *client;
	gint width, height;

	//cout << "vc_config_save" << endl;

	client = guiStr.client;
	if( client == NULL )
		return;

	if( guiStr.main_window ) {
		gdk_window_get_size (guiStr.main_window->window, &width, &height);

		gconf_client_set_int (client, "/apps/vcgui/width", width, NULL);
		gconf_client_set_int (client, "/apps/vcgui/height", height, NULL);
		//cout << "-- width=" << width << " height=" << height << endl;
	}

	char *tmpStr;
	String f;
	GError *error = NULL;

	int n = 0;
	if( guiStr.config.gpsmap ) {
		n = guiStr.config.gpsmap->getNumRaster();
		if( n == 0 ) {
			(void)gconf_client_unset( client, "/apps/vcgui/raster/num_maps", &error);
		} else  {
			gconf_client_set_int(client, "/apps/vcgui/raster/num_maps", n, NULL);

			for( int i=0; i < n; i++ ) {
				f = guiStr.config.gpsmap->getRasterFile(i);
				tmpStr = g_strdup_printf("/apps/vcgui/raster/%i", i);
				gconf_client_set_string (client, tmpStr, f.c_str(), NULL);
				g_free(tmpStr);
			}
		}
	}

	// configuration files
	gconf_client_set_string (client, "/apps/vcgui/glade_file",         guiStr.config.glade_file.c_str(), NULL);
	gconf_client_set_string (client, "/apps/vcgui/menu_ui_file",       guiStr.config.menu_ui_file.c_str(), NULL);
	gconf_client_set_string (client, "/apps/vcgui/radar_station_file", guiStr.config.radar_station_file.c_str(), NULL);
	gconf_client_set_string (client, "/apps/vcgui/node_ui_file",       guiStr.config.node_ui_file.c_str(), NULL);
	
	// now force sync
	gconf_client_suggest_sync (client, NULL);
}

void vc_config_save_chart( GtkStripChart * g) 
{
	char name[256]={'\0'};

	int width, height;
	width = g->parent->allocation.width;
	height = g->parent->allocation.height;

	sprintf(name, "%s/width", g->config.gconf_str);
	gconf_client_set_int (guiStr.client, name, width, NULL);

	sprintf(name, "%s/height", g->config.gconf_str);
	gconf_client_set_int (guiStr.client, name, height, NULL);

	gconf_client_suggest_sync (guiStr.client, NULL);
}

// function to set the correct colors for the widget hold node treeview
void setColor(GtkWidget *widget) 
{
	GdkColor color, text;
	gdk_color_parse ("#DCDAD5", &color);
	gdk_color_parse ("black", &text);

	gtk_widget_modify_base (GTK_WIDGET(widget), GTK_STATE_NORMAL, &color);
	gtk_widget_modify_base (GTK_WIDGET(widget), GTK_STATE_ACTIVE, &color);
	gtk_widget_modify_base (GTK_WIDGET(widget), GTK_STATE_SELECTED, &color);

	gtk_widget_modify_text (GTK_WIDGET(widget), GTK_STATE_NORMAL, &text);
	gtk_widget_modify_text (GTK_WIDGET(widget), GTK_STATE_ACTIVE, &text);
	gtk_widget_modify_text (GTK_WIDGET(widget), GTK_STATE_SELECTED, &text);
}


